<?php
	if($_SERVER['HTTPS']!="on")
	{
		$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		header("Location:$redirect");
	}
	
	require_once(dirname(__FILE__)) . "/config/cConfig.php";
	
	$webConfig = new \ampleharvest\cConfig();
	
	$type = '';
	if(array_key_exists('type', $_REQUEST)) {
		$type = $_REQUEST['type'];
	}
	$ID = '';
	if(isset($_REQUEST['id'])) {
		$ID = $_REQUEST['id'];
	}
	$view = '';
	if(isset($_REQUEST['view'])) {
		$view = $_REQUEST['view'];
	}
	
	$apache_user = apache_getenv('SSL_CLIENT_S_DN');
?>
<html>
	<head>
		<title><?php print($webConfig->C_STR_TITLE)?></title>	
	</head>
	<body>

	<?php require('ext-setup.php')?>
	
	<script language="javascript">
			Ext.onReady(Setup);
		
			function Setup()
			{
		
				var dn = '<?php print($apache_user)?>';

				Ext.Ajax.request({
					// TODO put this in a config file
				    url: '/user_validation/users/show/',
				    method : 'POST',
				    params: {
				        dn : dn
				    },
				    success: function(response){
				        obj_config.user = Ext.decode(response.responseText);

				        obj_config.C_STR_APPLICATION 	= '<?php print($type); ?>';
						var str_id_from_request 		= '<?php print($ID); ?>';
						var str_view_force				= '<?php print($view); ?>';
						
						if(!isValidType(obj_config.C_STR_APPLICATION)) {
							obj_config.C_STR_APPLICATION = TYPE.AMPLEHARVEST;
						}

						var str_page_path = 'AmpleHarvest.views.';

									
						if(str_view_force && isValidView(str_view_force))
						{
							str_page_path += str_view_force;
						} else {
							str_page_path += obj_config.C_STR_APPLICATION.toLowerCase();
						}	
						str_page_path += '.main';

						var m_obj_Page = Ext.create(str_page_path,
								{
									m_str_id : str_id_from_request
								});
						
						m_obj_AmpleHarvest_ViewPort = Ext.create('AmpleHarvest.layout.cViewPort', 
						{
							configObject	: obj_config,
							contentItems 	: m_obj_Page,
				    		renderTo		: Ext.getBody()
				    	});
				    },
				    failure: function(response){
					    console.log('User Validation service failed to respond');
					    console.log(response);
				    }
				});
			}

			function isValidType(str_Type) {
				return TYPE[str_Type];
			}

			function isValidView(str_View) {
				return VIEW[str_View];
			}
		</script>
	</body>
</html>
